package com.study.controller;


import com.study.bean.query.BuildingQuery;
import com.study.service.BuildingService;
import com.study.service.impl.BuildingServiceImpl;
import com.study.util.MyBeanUtils;
import com.study.util.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/building/*")
public class BuildingController extends BaseController {


    private final BuildingService buildingService = new BuildingServiceImpl();



    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");

        switch (method){
            case "selectByPage":
                selectByPage(req, resp);
                break;
            //case "addBuilding":
            //    addBuilding(req,resp);
            //    break;
            default:
                break;
        }
    }


    //private void addBuilding(HttpServletRequest req, HttpServletResponse resp) {
    //    System.out.println("BuildingController.addBuilding");
    //    Building building = MyBeanUtils.copyToBean(req, Building.class);
    //    Boolean b = buildingService.addBuilding(building);
    //    if (b){
    //        PageInfo pageInfo = new PageInfo(0, b);
    //        toJSON(resp, pageInfo);
    //    }else {
    //        PageInfo pageInfo = new PageInfo(1, b);
    //        toJSON(resp, pageInfo);
    //    }
    //
    //}
    //
    //
    //
    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogServlet.selectByPage");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        BuildingQuery blogQuery = new BuildingQuery(Integer.parseInt(page), Integer.parseInt(limit));
        BuildingQuery buildingQuery = MyBeanUtils.copyToBean(req, BuildingQuery.class);
        System.out.println(buildingQuery);
        PageInfo pageInfo = buildingService.selectByPage(buildingQuery);
        //JSONUtils.toJSON(resp, pageInfo);
        toJSON(resp, pageInfo);
    }
}
